package com.amumu.netctoss.interceptor;


import org.hibernate.Session;
import org.hibernate.Transaction;

import com.amumu.netctoss.util.HibernateUtil;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class OpenSessionInViewInterceptor extends AbstractInterceptor{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String intercept(ActionInvocation arg0) throws Exception {
		Session session = HibernateUtil.getCurrentSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			arg0.invoke();
			if(!tx.isActive()){
				tx.commit();
			}
			return null;
		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
			throw e;
		} finally{
			HibernateUtil.closeSession();
		}
	}

}
